package com.bh.web.controller.ledger;

import com.bh.common.annotation.Log;
import com.bh.common.constant.VerifiZbStatus;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.ledger.domain.JcVerificationRecord;
import com.bh.ledger.domain.JcVerificationZb;
import com.bh.ledger.service.IJcVerificationZbService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 核销记录主表Controller
 * 
 * @author slx
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/ledger/verification")
public class JcVerificationZbController extends BaseController
{
    @Autowired
    private IJcVerificationZbService jcVerificationZbService;

    /**
     * 查询核销记录主表列表
     */
    @ApiOperation("查询核销记录主表列表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcVerificationZb jcVerificationZb)
    {
        startPage();
        jcVerificationZb.setJcOrganizationId(getOrgId());
        List<JcVerificationZb> list = jcVerificationZbService.selectJcVerificationZbList(jcVerificationZb);
        return getDataTable(list);
    }

    /**
     * 导出核销记录主表列表
     */
    @PreAuthorize("@ss.hasPermi('ledger:verification:export')")
    @Log(title = "核销记录主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcVerificationZb jcVerificationZb)
    {
        List<JcVerificationZb> list = jcVerificationZbService.selectJcVerificationZbList(jcVerificationZb);
        ExcelUtil<JcVerificationZb> util = new ExcelUtil<>(JcVerificationZb.class);
        util.exportExcel(response, list, "核销记录主表数据");
    }


    /**
     * 查询核销记录主表列表
     */
    @ApiOperation("查询收入核销记录列表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:list')")
    @GetMapping("/incomeList")
    public TableDataInfo incomeList(JcVerificationZb jcVerificationZb)
    {
        startPage();
        jcVerificationZb.setJcOrganizationId(getOrgId());
        List<JcVerificationZb> list = jcVerificationZbService.selectIncomeList(jcVerificationZb);
        return getDataTable(list);
    }

    /**
     * 成本核销记录列表
     */
    @ApiOperation("查询成本核销记录列表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:list')")
    @GetMapping("/costList")
    public TableDataInfo costList(JcVerificationZb jcVerificationZb)
    {
        startPage();
        jcVerificationZb.setJcOrganizationId(getOrgId());
        List<JcVerificationZb> list = jcVerificationZbService.selectCostList(jcVerificationZb);
        return getDataTable(list);
    }


    /**
     * 成本核销记录列表
     */
    @ApiOperation("查询成本核销记录列表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:list')")
    @GetMapping("/dispatchList")
    public TableDataInfo dispatchList(JcVerificationZb jcVerificationZb)
    {
        startPage();
        jcVerificationZb.setJcOrganizationId(getOrgId());
        List<JcVerificationZb> list = jcVerificationZbService.selectDispatchList(jcVerificationZb);
        return getDataTable(list);
    }


    /**
     * 获取核销记录主表详细信息
     */
    @ApiOperation("获取核销记录主表详细信息")
    @PreAuthorize("@ss.hasPermi('ledger:verification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jcVerificationZbService.selectJcVerificationZbById(id));
    }

    /**
     * 新增核销记录主表
     */
    @ApiOperation("新增核销记录主表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:add')")
    @Log(title = "核销记录主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcVerificationZb jcVerificationZb)
    {
        return toAjax(jcVerificationZbService.insertJcVerificationZb(jcVerificationZb));
    }

    /**
     * 修改核销记录主表
     */
    @ApiOperation("修改核销记录主表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:edit')")
    @Log(title = "核销记录主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcVerificationZb jcVerificationZb)
    {
        return toAjax(jcVerificationZbService.updateJcVerificationZb(jcVerificationZb));
    }

    /**
     * 删除核销记录主表
     */
    @ApiOperation("删除核销记录主表")
    @PreAuthorize("@ss.hasPermi('ledger:verification:remove')")
    @Log(title = "核销记录主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jcVerificationZbService.deleteJcVerificationZbByIds(ids));
    }


    /**
     * 新增核销记录主表
     */
    @ApiOperation("新增核销记录")
    @PreAuthorize("@ss.hasPermi('ledger:verification:add')")
    @Log(title = "核销记录", businessType = BusinessType.INSERT)
    @PostMapping("/record")
    public AjaxResult addRecord(@RequestBody JcVerificationRecord verificationRecord)
    {
        if (verificationRecord.getJcVerificationzbId() == null) {
            return error("核销记录主表ID不能为空");
        }

        if (verificationRecord.getYhxMoney().doubleValue() > verificationRecord.getZmoney().doubleValue()) {
            return error("核销金额不能大于未核销金额");
        }

        if (verificationRecord.getHxMoney().doubleValue() < 0) {
            return error("核销金额不能为负数");
        }
        JcVerificationZb verificationZb = jcVerificationZbService.selectJcVerificationZbById(verificationRecord.getJcVerificationzbId());
        if (verificationZb.getHxStatus() == VerifiZbStatus.ACTIVE) {
            return error("核销记录已核销完成");
        }

        verificationRecord.setOperator(SecurityUtils.getUsername());
        if (verificationRecord.getZmoney().equals(verificationRecord.getHxMoney())) {
            verificationRecord.setHxType(1);
        }
        if (verificationRecord.getZmoney().compareTo(verificationRecord.getHxMoney()) > 0) {
            verificationRecord.setHxType(2);
        }
        return toAjax(jcVerificationZbService.insertJcVerificationRecord(verificationRecord));
    }

    /**
     * 修改核销记录主表
     */
    @ApiOperation("修改核销记录")
    @PreAuthorize("@ss.hasPermi('ledger:verification:edit')")
    @Log(title = "核销记录", businessType = BusinessType.UPDATE)
    @PutMapping("/record")
    public AjaxResult editRecord(@RequestBody JcVerificationRecord jcVerificationRecord)
    {
        return toAjax(jcVerificationZbService.updateJcVerificationRecord(jcVerificationRecord));
    }

}
